package com.ruoyi.incident.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 事件Controller
 *
 * @author wang
 * @date 2022-01-13
 */
@RestController
@RequestMapping("/incident/incident")
public class XlkjIncidentController extends BaseController
{
    @Autowired
    private IXlkjIncidentService xlkjIncidentService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询事件列表
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjIncident xlkjIncident)
    {
        startPage();
        if(xlkjIncident.getDepartmentId() != null && xlkjIncident.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjIncident.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjIncident.setDepartmentList(str);
        }
        List<XlkjIncident> list = xlkjIncidentService.selectXlkjIncidentList(xlkjIncident);
        for(XlkjIncident incident:list){
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(incident.getDepartmentId())).getDeptName();
            incident.setDepartmentId(deptName);
            if(incident.getAssist().equals("Y")){
                //是否需要该用户协助
                Long userId = SecurityUtils.getUserId();
                incident.setUserHelp(userId == Long.valueOf(incident.getAssistDepartment()));
                //设置协管部门人员
                incident.setAssistDepartment(sysUserService.selectUserById(Long.valueOf(incident.getAssistDepartment())).getNickName());
            }


        }
        return getDataTable(list);
    }

    /**
     * 导出事件列表
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:export')")
    @Log(title = "事件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjIncident xlkjIncident)
    {
        List<XlkjIncident> list = xlkjIncidentService.selectXlkjIncidentList(xlkjIncident);
        ExcelUtil<XlkjIncident> util = new ExcelUtil<XlkjIncident>(XlkjIncident.class);
        return util.exportExcel(list, "事件数据");
    }

    /**
     * 获取事件详细信息
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjIncidentService.selectXlkjIncidentById(id));
    }

    /**
     * 新增事件
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:add')")
    @Log(title = "事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjIncident xlkjIncident)
    {
        return toAjax(xlkjIncidentService.insertXlkjIncident(xlkjIncident));
    }

    /**
     * 修改事件
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:edit')")
    @Log(title = "事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjIncident xlkjIncident)
    {
        return toAjax(xlkjIncidentService.updateXlkjIncident(xlkjIncident));
    }

    /**
     * 删除事件
     */
    @PreAuthorize("@ss.hasPermi('incident:incident:remove')")
    @Log(title = "事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjIncidentService.deleteXlkjIncidentByIds(ids));
    }
}
